package ding;

import work.config.Constant;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DeWeight {

    private static File moveFile = new File(Constant.work,"原始文件-格式化后.txt");

    private static File repetitiveFile = new File(Constant.work, "原始文件-格式化后_重复文件.txt");

    private static File notRepetitiveFile = new File(Constant.work, "原始文件-格式化后_没有重复的文件.txt");

    public static void main(String[] args) throws IOException {

        if (!repetitiveFile.exists()) {
            repetitiveFile.createNewFile();
        }

        if (!notRepetitiveFile.exists()) {
            notRepetitiveFile.createNewFile();
        }

        BufferedReader reader = new BufferedReader(new FileReader(moveFile));

        Map<String, List<String>> rs = new HashMap<>();

        String line;

        while ((line = reader.readLine()) != null) {

            String[] split = line.split("->");

            String from = split[0];

            String to = split[1];

            //添加重复计数
            List<String> strings = rs.computeIfAbsent(to, k -> new ArrayList<>());

            strings.add(from);

        }


        BufferedWriter r = new BufferedWriter(new FileWriter(repetitiveFile));
        BufferedWriter n = new BufferedWriter(new FileWriter(notRepetitiveFile));

        rs.forEach((k, list) -> {

            if (list.size() == 1) {
                try {
                    n.write(k + "\n");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {

                StringBuilder s = new StringBuilder(k + "=>" + list.get(0));

                for (int i = 1; i < list.size(); i++) {
                    s.append("|").append(list.get(i));
                }

                try {
                    r.write(s + "\n");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        });

        r.flush();
        n.flush();
        r.close();
        n.close();

    }

}
